package stack1;

import java.util.Stack;

//括号匹配
public class Stack2 {
    public static boolean isValid(String str) {
        Stack<Character> stack = new Stack<>();
        for(int i=0;i<str.length();i++){
            char ch=str.charAt(i);
            if(ch=='{'||ch=='['||ch=='('||ch=='<'){
                stack.push(ch);
            }
            else{
                if(stack.empty()){
                    return false;
                }
                char ch2=stack.peek();
                if(ch2=='{'&&ch=='}'||ch2=='['&&ch==']'||ch2=='('&&ch==')'||ch2=='<'&&ch=='>'){
                    stack.pop();
                }else{
                    return false;
                }
            }
        }
        if(!stack.empty()){
            return false;
        }
        return true;
    }
    public static void main(String[] args) {
        String str="({[>]})";
        System.out.println(isValid(str));
    }
}
